package com.czxy.demo05;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by hp on 2020/2/2.
 */
public class Test30 {
    public static void main(String[] args) throws IOException {
        boolean login = login();
        otherLogin();
        if (login!=true){
            System.out.println("登入失败！！");
        }

    }
    public static void otherLogin() throws IOException {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        Date date = new Date();
        StringBuilder  sb = new StringBuilder();
        sb.append("游客").append(df.format(date)).append("  进行浏览！").append("\r\n");
        FileUtils.writeStringToFile(new File("data2.txt"),sb.toString(),true);

    }
    public static boolean login() throws IOException {
        List<String> lines = FileUtils.readLines(new File("user.properties"));
        HashMap<String,String> map = new HashMap<>();
        for (String s : lines) {
            String[] ss = s.split("=");
            map.put(ss[0],ss[1]);
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String s = sc.nextLine();
        System.out.println("请输入用户密码：");
        String s1 = sc.nextLine();
        StringBuilder  sb = new StringBuilder();
        StringBuilder  sb1 = new StringBuilder();
        DateFormat  df = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        int b = -1;
        for (Map.Entry<String, String> e : map.entrySet()) {
            if (s.equals(e.getKey())&&s1.equals(e.getValue())){
                b=0;
            }else {
            }
        }
        if (b==-1){
            Date date = new Date();
            sb1.append(s).append(" ").append(df.format(date)).append(" 登入失败！！").append("\r\n");
            FileUtils.writeStringToFile(new File("data2.txt"),sb1.toString(),true);
            return false;
        }else {
            Date date = new Date();
            sb.append(s).append(" ").append(df.format(date)).append(" 登入成功！！").append("\r\n");
            FileUtils.writeStringToFile(new File("data2.txt"),sb.toString(),true);
            return true;}
    }
}
